﻿package com.tudou.utils
{
    import com.tudou.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.*;
    import flash.utils.*;

    public class LoaderBase extends EventDispatcher
    {
        private var request:URLRequest;
        private var _bytesLoaded:uint = 0;
        private var _loaderContext:LoaderContext;
        private var _timeout:int = 0;
        private var _tryTimes:int = 1;
        private var _url:String;
        private var isLoading:Boolean = false;
        private var _httpStatus:String;
        private var _bytesTotal:uint = 0;
        private var _timeoutTimer:Timer;
        private var displayObjectLoaderTypes:Array;
        public var debug:Boolean = false;
        private var isUrlLoader:Boolean = false;
        private var _loador:Object;
        private var _tryCount:int = 0;
        private var variables:URLVariables;

        public function LoaderBase(param1:Boolean = false)
        {
            displayObjectLoaderTypes = ["swf", "jpg", "png", "gif"];
            this.isUrlLoader = param1;
            return;
        }// end function

        protected function progressHlr(event:ProgressEvent) : void
        {
            this._bytesLoaded = event.bytesLoaded;
            this._bytesTotal = event.bytesTotal;
            return;
        }// end function

        private function removeTimeoutTimer() : void
        {
            if (_timeoutTimer)
            {
                _timeoutTimer.stop();
                _timeoutTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, timeoutErrorHlr);
            }
            _timeoutTimer = null;
            return;
        }// end function

        protected function ioErrorHlr(event:IOErrorEvent) : void
        {
            isLoading = false;
            var _loc_2:* = _httpStatus ? (_httpStatus) : (event.toString());
            dispatchEvent(new LoaderEvent(LoaderEvent.IO_ERROR, false, false, _loc_2));
            retry();
            return;
        }// end function

        private function _debuger(param1:String) : void
        {
            if (!debug)
            {
                return;
            }
            trace("_debuger:" + param1);
            return;
        }// end function

        public function set tryTimes(param1:int) : void
        {
            _tryTimes = param1;
            return;
        }// end function

        public function get bytesTotal() : uint
        {
            return _bytesTotal;
        }// end function

        protected function addEvents() : void
        {
            var _loc_1:IEventDispatcher = null;
            if (!isUrlLoader)
            {
                _loc_1 = _loador.contentLoaderInfo as LoaderInfo;
                _debuger("o = _loador.contentLoaderInfo;");
            }
            else
            {
                _loc_1 = _loador;
            }
            _loc_1.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHlr);
            _loc_1.addEventListener(ProgressEvent.PROGRESS, progressHlr);
            _loc_1.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHlr);
            _loc_1.addEventListener(Event.COMPLETE, completeHlr);
            if (isUrlLoader)
            {
                _loc_1.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHlr);
            }
            return;
        }// end function

        private function _end() : void
        {
            dispatchEvent(new LoaderEvent(LoaderEvent.END));
            return;
        }// end function

        private function addTimer() : void
        {
            if (_timeoutTimer)
            {
                _timeoutTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeoutErrorHlr);
                _timeoutTimer.start();
            }
            return;
        }// end function

        protected function securityErrorHlr(event:SecurityErrorEvent) : void
        {
            isLoading = false;
            dispatchEvent(new LoaderEvent(LoaderEvent.SECURITY_ERROR, false, false, event.toString()));
            retry();
            return;
        }// end function

        protected function retry() : void
        {
            if (_tryCount < _tryTimes)
            {
                _load();
            }
            else
            {
                close();
                _end();
            }
            return;
        }// end function

        public function get data()
        {
            if (_loador == null)
            {
                return null;
            }
            if (isUrlLoader)
            {
                return _loador.data;
            }
            return _loador.content;
        }// end function

        public function set loaderContext(param1:LoaderContext) : void
        {
            _loaderContext = param1;
            return;
        }// end function

        protected function removeEvents() : void
        {
            var _loc_1:IEventDispatcher = null;
            if (!_loador)
            {
                return;
            }
            if (!isUrlLoader)
            {
                _loc_1 = _loador.contentLoaderInfo;
            }
            else
            {
                _loc_1 = _loador;
            }
            _loc_1.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHlr);
            _loc_1.removeEventListener(ProgressEvent.PROGRESS, progressHlr);
            _loc_1.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHlr);
            _loc_1.removeEventListener(Event.COMPLETE, completeHlr);
            if (isUrlLoader)
            {
                _loc_1.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHlr);
            }
            return;
        }// end function

        private function _loader() : void
        {
            _debuger("_loader");
            request = new URLRequest(_url);
            _load();
            return;
        }// end function

        public function set timeout(param1:int) : void
        {
            _timeout = param1;
            return;
        }// end function

        protected function timeoutErrorHlr(event:TimerEvent) : void
        {
            dispatchEvent(new LoaderEvent(LoaderEvent.TIMEOUT_ERROR, false, false, LoaderEvent.TIMEOUT_ERROR));
            retry();
            return;
        }// end function

        private function _load() : void
        {
            close();
            var _loc_2:* = _tryCount + 1;
            _tryCount = _loc_2;
            if (hasTimeout)
            {
                _timeoutTimer = new Timer(_timeout, 1);
            }
            if (!isUrlLoader)
            {
                _loador = new Loader();
                _prepareLoad();
                _loador.load(request, _loaderContext);
            }
            else
            {
                _loador = new URLLoader();
                _prepareLoad();
                _loador.load(request);
            }
            return;
        }// end function

        public function clear() : void
        {
            if (!isUrlLoader && _loador != null)
            {
                try
                {
                    _loador.unload();
                }
                catch (e:Error)
                {
                }
            }
            close();
            return;
        }// end function

        private function _prepareLoad() : void
        {
            addEvents();
            addTimer();
            isLoading = true;
            return;
        }// end function

        public function get bytesLoaded() : uint
        {
            return _bytesLoaded;
        }// end function

        private function _urlLoader(param1:Boolean = false, param2:Object = null) : void
        {
            var _loc_3:String = null;
            _debuger("_urlLoader");
            request = new URLRequest();
            variables = new URLVariables();
            if (param2 != null && param2 != "")
            {
                for (_loc_3 in param2)
                {
                    
                    variables[_loc_3] = encodeURIComponent(String(param2[_loc_3]));
                }
            }
            if (param1)
            {
                request.method = URLRequestMethod.POST;
            }
            request.url = _url;
            request.data = unescape(variables.toString());
            _load();
            return;
        }// end function

        private function get hasTimeout() : Boolean
        {
            return _timeout > 0;
        }// end function

        public function load(param1:String, param2:Boolean = false, param3:Object = null) : void
        {
            _url = param1;
            if (!_url)
            {
                dispatchEvent(new LoaderEvent(LoaderEvent.IO_ERROR, false, false, "url null"));
                _end();
                return;
            }
            if (isUrlLoader)
            {
                _urlLoader(param2, param3);
            }
            else
            {
                _loader();
            }
            return;
        }// end function

        public function get url() : String
        {
            return _url;
        }// end function

        protected function httpStatusHlr(event:HTTPStatusEvent) : void
        {
            switch(event.status)
            {
                case 0:
                case 200:
                {
                    break;
                }
                default:
                {
                    _httpStatus = event.status.toString();
                    break;
                    break;
                }
            }
            return;
        }// end function

        protected function completeHlr(event:Event) : void
        {
            isLoading = false;
            removeTimeoutTimer();
            removeEvents();
            dispatchEvent(new LoaderEvent(LoaderEvent.COMPLETE));
            _end();
            return;
        }// end function

        public function close() : void
        {
            removeTimeoutTimer();
            if (_loador == null)
            {
                return;
            }
            if (isLoading)
            {
                try
                {
                    _loador.close();
                }
                catch (e:Error)
                {
                }
            }
            _bytesLoaded = 0;
            _bytesTotal = 0;
            isLoading = false;
            removeEvents();
            _loaderContext = null;
            _loador = null;
            _httpStatus = null;
            return;
        }// end function

        public function get content() : DisplayObject
        {
            if (_loador == null || isUrlLoader)
            {
                return null;
            }
            return _loador.content;
        }// end function

    }
}
